package GUILayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import ControlLayer.*;
import ModelLayer.*;

public class CustomerDetails extends JFrame {

	private JPanel contentPane;
	private JTextField txtFName;
	private JTextField txtLName;
	private JTextField txtCPR;
	private JTextField txtAddress;
	private JTextField txtCity;
	private JTextField txtZIP;
	private JTextField txtPhone;
	private JTextField txtMail;
	private JTextField txtName;
	private JTextField txtCVR;
	
	private JPanel panelPrivate;
	private JPanel panelCompany;
	
	private IFSearch search;
	private IFUpdate update;
	
	private ModelLayer.Contact contact = new ModelLayer.Contact();
	private ModelLayer.Private privateC = new ModelLayer.Private();
	private ModelLayer.Business businessC = new ModelLayer.Business();
	
	private boolean errorP;
	private boolean errorZ;

	/**
	 * Create the frame.
	 */
	public CustomerDetails(int id, boolean isPrivate, boolean isArchive) {
		
		search = new SearchControler();
		update = new UpdateControler();
		
		contact = search.getCustomerById(id);		
		if(contact instanceof Private) {
		privateC = (Private) contact;
		}
		else {
		businessC = (Business) contact;
		}
		
		setResizable(false);
		setTitle("Customer");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(150, 50, 300, 360);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		drawPrivate(id, isArchive);
		drawCompany(id, isArchive);
		
		if(isPrivate)
		{
			panelPrivate.setVisible(true);
		}
		else
		{
			panelCompany.setVisible(true);
		}
		
		
			
	}
	private void drawPrivate(final int id, boolean isArchive)
	{
		panelPrivate = new JPanel();
		panelPrivate.setBounds(10, 45, 278, 275);
		panelPrivate.setBackground(new Color(255, 255, 204));
		contentPane.add(panelPrivate);
		panelPrivate.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 89, 14);
		panelPrivate.add(lblId);
			
		JLabel lblFirstName = new JLabel("First name:");
		lblFirstName.setBounds(10, 36, 89, 14);
		panelPrivate.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last name:");
		lblLastName.setBounds(10, 61, 89, 14);
		panelPrivate.add(lblLastName);
		
		JLabel lblCpr = new JLabel("CPR:");
		lblCpr.setBounds(10, 86, 89, 14);
		panelPrivate.add(lblCpr);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(10, 111, 89, 14);
		panelPrivate.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(10, 136, 89, 14);
		panelPrivate.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(10, 161, 89, 14);
		panelPrivate.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 186, 89, 14);
		panelPrivate.add(lblPhone);
		
		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(10, 211, 89, 14);
		panelPrivate.add(lblEmail);
		
		txtFName = new JTextField();
		txtFName.setText(privateC.getName());
		txtFName.setBounds(121, 33, 118, 20);
		panelPrivate.add(txtFName);
		txtFName.setColumns(10);
		if(isArchive)
		{
			txtFName.setEditable(false);
		}
		
		txtLName = new JTextField();
		txtLName.setText(privateC.getSurname());
		txtLName.setBounds(121, 58, 118, 20);
		panelPrivate.add(txtLName);
		txtLName.setColumns(10);
		if(isArchive)
		{
			txtLName.setEditable(false);
		}
		
		txtCPR = new JTextField();
		txtCPR.setText(privateC.getCPR());
		txtCPR.setBounds(121, 83, 118, 20);
		panelPrivate.add(txtCPR);
		txtCPR.setColumns(10);
		if(isArchive)
		{
			txtCPR.setEditable(false);
		}
		
		txtAddress = new JTextField();
		txtAddress.setText(privateC.getAddress());
		txtAddress.setBounds(121, 108, 118, 20);
		panelPrivate.add(txtAddress);
		txtAddress.setColumns(10);
		if(isArchive)
		{
			txtAddress.setEditable(false);
		}
		
		txtCity = new JTextField();
		txtCity.setText(privateC.getCity());
		txtCity.setBounds(121, 133, 118, 20);
		panelPrivate.add(txtCity);
		txtCity.setColumns(10);
		if(isArchive)
		{
			txtCity.setEditable(false);
		}
		
		txtZIP = new JTextField();
		txtZIP.setText(privateC.getZipCode());
		txtZIP.setBounds(121, 158, 118, 20);
		panelPrivate.add(txtZIP);
		txtZIP.setColumns(10);
		if(isArchive)
		{
			txtZIP.setEditable(false);
		}
		
		txtPhone = new JTextField();
		txtPhone.setText(privateC.getPhoneNO());
		txtPhone.setBounds(121, 183, 118, 20);
		panelPrivate.add(txtPhone);
		txtPhone.setColumns(10);
		if(isArchive)
		{
			txtPhone.setEditable(false);
		}
		
		txtMail = new JTextField();
		txtMail.setText(privateC.getEMail());
		txtMail.setBounds(121, 208, 118, 20);
		panelPrivate.add(txtMail);
		txtMail.setColumns(10);
		if(isArchive)
		{
			txtMail.setEditable(false);
		}
		
		JLabel lblShowID = new JLabel("");
		lblShowID.setText(Integer.toString(id));
		lblShowID.setBounds(121, 11, 118, 14);
		panelPrivate.add(lblShowID);
		
		if(!isArchive){
			JButton btnSave = new JButton("Save");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					saveEditedPrivate(id, txtFName.getText(), txtLName.getText(), txtCPR.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText(), txtPhone.getText(), txtMail.getText());
					dispose();
				}
			});
			btnSave.setBounds(10, 241, 89, 23);
			panelPrivate.add(btnSave);
		}
		else
		{
			JButton btnActivate = new JButton("Activate");
			btnActivate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					privateC.setStatus("active");
					update.updateCustomer(privateC);
					dispose();
					MainWindow.getInstance().refreshTableCustomer();
					MainWindow.getInstance().refreshAC();
				}
			});
			btnActivate.setBounds(10, 241, 89, 23);
			panelPrivate.add(btnActivate);
		}
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setBounds(179, 241, 89, 23);
		panelPrivate.add(btnCancel);
		
		panelPrivate.setVisible(false);
		
	}
	
	private void drawCompany(final int id, boolean isArchive)
	{
		panelCompany = new JPanel();
		panelCompany.setBackground(new Color(255, 255, 204));
		panelCompany.setBounds(10, 45, 278, 275);
		contentPane.add(panelCompany);
		panelCompany.setLayout(null);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 89, 14);
		panelCompany.add(lblId);
		
		JLabel lblShowID = new JLabel("");
		lblShowID.setText(Integer.toString(id));
		lblShowID.setBounds(121, 11, 118, 14);
		panelCompany.add(lblShowID);
		
		JLabel lblFirstName = new JLabel("Name:");
		lblFirstName.setBounds(10, 36, 89, 14);
		panelCompany.add(lblFirstName);
		
		JLabel lblCpr = new JLabel("CVR:");
		lblCpr.setBounds(10, 61, 89, 14);
		panelCompany.add(lblCpr);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(10, 86, 89, 14);
		panelCompany.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(10, 111, 89, 14);
		panelCompany.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(10, 136, 89, 14);
		panelCompany.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 161, 89, 14);
		panelCompany.add(lblPhone);
		
		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(10, 186, 89, 14);
		panelCompany.add(lblEmail);
		
		txtName = new JTextField();
		txtName.setText(businessC.getName());
		txtName.setBounds(121, 33, 118, 20);
		panelCompany.add(txtName);
		txtName.setColumns(10);
		if(isArchive)
		{
			txtName.setEditable(false);
		}
		
		txtCVR = new JTextField();
		txtCVR.setText(businessC.getCVR());
		txtCVR.setBounds(121, 58, 118, 20);
		panelCompany.add(txtCVR);
		txtCVR.setColumns(10);
		if(isArchive)
		{
			txtCVR.setEditable(false);
		}
		
		txtAddress = new JTextField();
		txtAddress.setText(businessC.getAddress());
		txtAddress.setBounds(121, 83, 118, 20);
		panelCompany.add(txtAddress);
		txtAddress.setColumns(10);
		if(isArchive)
		{
			txtAddress.setEditable(false);
		}
		
		txtCity = new JTextField();
		txtCity.setText(businessC.getCity());
		txtCity.setBounds(121, 108, 118, 20);
		panelCompany.add(txtCity);
		txtCity.setColumns(10);
		if(isArchive)
		{
			txtCity.setEditable(false);
		}
		
		txtZIP = new JTextField();
		txtZIP.setText(businessC.getZipCode());
		txtZIP.setBounds(121, 133, 118, 20);
		panelCompany.add(txtZIP);
		txtZIP.setColumns(10);
		if(isArchive)
		{
			txtZIP.setEditable(false);
		}
		
		txtPhone = new JTextField();
		txtPhone.setText(businessC.getPhoneNO());
		txtPhone.setBounds(121, 158, 118, 20);
		panelCompany.add(txtPhone);
		txtPhone.setColumns(10);
		if(isArchive)
		{
			txtPhone.setEditable(false);
		}
		
		txtMail = new JTextField();
		txtMail.setText(businessC.getEMail());
		txtMail.setBounds(121, 183, 118, 20);
		panelCompany.add(txtMail);
		txtMail.setColumns(10);
		if(isArchive)
		{
			txtMail.setEditable(false);
		}
		
		if(!isArchive)
		{
			JButton btnSave = new JButton("Save");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					saveEditedBusiness(id, txtName.getText(), txtCVR.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText(), txtPhone.getText(), txtMail.getText());
					dispose();
				}
			});
			btnSave.setBounds(10, 241, 89, 23);
			panelCompany.add(btnSave);
		}
		else
		{
			JButton btnActivate = new JButton("Activate");
			btnActivate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					businessC.setStatus("active");
					update.updateCustomer(businessC);
					dispose();
					MainWindow.getInstance().refreshTableCustomer();
					MainWindow.getInstance().refreshAC();
				}
			});
			btnActivate.setBounds(10, 241, 89, 23);
			panelCompany.add(btnActivate);
		}
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setBounds(179, 241, 89, 23);
		panelCompany.add(btnCancel);
		
		
		panelCompany.setVisible(false);
	}
	
	private void saveEditedPrivate(int id, String fName, String lName, String cpr, String address, String city, String zip, String phone, String eMail)
	{
		if(fName.isEmpty() || lName.isEmpty() || cpr.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty() || phone.isEmpty() || eMail.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorP=false;
            try
            {
                    Integer.valueOf(phone);
            }
            catch(Exception e)
            {
                    JOptionPane.showMessageDialog(null,
                                    "The phone number should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                    errorP=true;
            }

            if(errorP!=true)
            {
                    errorZ=false;
                    try
                    {
                            Integer.valueOf(zip);
                    }
                    catch(Exception e)
                    {
                            JOptionPane.showMessageDialog(null,
                                            "The ZIP code should be a number!",
                                            "Warning",
                                            JOptionPane.WARNING_MESSAGE);
                            errorZ=true;
                    }

                    if(errorZ!=true)
                    {       
                            ModelLayer.Private p = new ModelLayer.Private(id, address, city, zip, phone, eMail, "status", cpr, fName, lName);
                            update.updateCustomer(p);
                    }
            }
		}
	}
	
	private void saveEditedBusiness(int id, String name, String cvr, String address, String city, String zip, String phone, String eMail)
	{
		if(name.isEmpty() || cvr.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty() || phone.isEmpty() || eMail.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
                    "All fields have to be filled.",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
		}
		else
		{
		    errorP=false;
            try
            {
                    Integer.valueOf(phone);
            }
            catch(Exception e)
            {
                    JOptionPane.showMessageDialog(null,
                                    "The phone number should be a number!",
                                    "Warning",
                                    JOptionPane.WARNING_MESSAGE);
                    errorP=true;
            }

            if(errorP!=true)
            {
                    errorZ=false;
                    try
                    {
                            Integer.valueOf(zip);
                    }
                    catch(Exception e)
                    {
                            JOptionPane.showMessageDialog(null,
                                            "The ZIP code should be a number!",
                                            "Warning",
                                            JOptionPane.WARNING_MESSAGE);
                            errorZ=true;
                    }

                    if(errorZ!=true)
                    {       
                            ModelLayer.Business b = new ModelLayer.Business(id, address, city, zip, phone, eMail, "active", cvr, name);
                            update.updateCustomer(b);
                    }
            }
		}
	}
}


